<?php
/**
 * @author Radovan Dvorsky
 * @version 20100326
 */
require_once ('application/default/models/Db/Mapper/MapperAbstract.php');

class Db_Mapper_Block extends Db_Mapper_Abstract {
	
		
	public function save(Db_Model_Abstract $block,$forceInsert = false){
		
		if(!$block instanceof Db_Model_Block){
			throw new Exception('$block is not instance of Db_Model_Block');
		}
		
		try{			
			
			$adapter = $this->getTable()->getAdapter();
			$adapter->beginTransaction();
			
			$bid = $this->getTable()->insert(array(
				'name' => $block->getName()
				)
			);
			
			if(count($block->getSubjects()) > 0){
				foreach($block->getSubjects() as $subject){
	
					$subjectBlockMapper = new Db_Mapper_SubjectBlock();
					$subjectBlockMapper->save(new Db_Model_SubjectBlock(array(
							'bid' => $bid,
							'sid' => $subject->getSid()
						)
					), 
					$forceInsert
					);					
				}			
			}
			
			$adapter->commit();
			
		} catch (Zend_Exception $e){
			$adapter->rollBack();
			throw new Db_Exception($e->getMessage());
		}		
	}
}

?>